﻿Public Class frmNewVendor 
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Public Primary_Key As Integer
    Public Section_ID As Integer
    Dim DTProvince, DTAMPHUR, DTTmb As New DataTable
#Region "Funtion"
    Function Load_ComboboxData_CompanyType() As Boolean
        Try
            Dim DS As New DataSet

            'Load_ComboboxData_site = False

            Strsql = "select vendor_type_id,vendor_type_name from vendorcompanytype order by vendor_type_id"

            DS = _Mysql.GetMYSQLDataset(Strsql, "DSvendor_type", "vendorcompanytype")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboCompanyType
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "vendor_type_name"
                .ValueMember = "vendor_type_id"
                .SelectionStart = 0
            End With
            ' Load_ComboboxData_site = True
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Function
    Sub Load_AddressData(Optional ByVal siteid As Integer = 0, Optional ByVal Str_Search As String = "")
        Try



            Strsql = "Select province_code,province_name from tblprovince"
            DTProvince = _Mysql.GetMYSQLDataTable(Strsql, "tblprovince")
            With Me.CboProvince
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTProvince
                .DisplayMember = "province_name"
                .ValueMember = "province_code"
            End With
            Strsql = "Select amphur_code,amphur_name from tblamphur where amphur_code like '" & Me.CboProvince.SelectedValue.ToString & "%'"
            DTAMPHUR = _Mysql.GetMYSQLDataTable(Strsql, "tblAMPHUR")

            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTAMPHUR
                .DisplayMember = "amphur_name"
                .ValueMember = "amphur_code"
            End With
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
            ' Cboonload = False
            On_Load = False

        Catch ex As Exception

        End Try
    End Sub
    Sub Load_Vendor_Type()
        Try
            Strsql = "select * from vendor_type"
            Dim DT_vendor_type As DataTable = _Mysql.GetMYSQLDataTable(Strsql, "vendor_type")
            With CboCompanyType
                .DataSource = DT_vendor_type
                .DisplayMember = "vendor_type_name"
                .ValueMember = "vendor_type_id"
            End With
        Catch ex As Exception

        End Try
    End Sub

#End Region
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.txtCompanyname.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
            Dim DT_Companny As New DataTable

            Strsql = "Select * from vendorcompany where companyname= '" & Me.txtCompanyname.Text & "'"

            DT_Companny = _Mysql.GetMYSQLDataTable(Strsql, "companyname")
            If DT_Companny.Rows.Count <> 0 Then
                MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                Exit Sub
            End If

            Strsql = "Insert into vendorcompany (companyname,companytype,address1,tmppart,amppart,chwpart,zipcode,tel,fax,note,active,taxid,contactname) "
            Strsql = Strsql & " values ('" & Me.txtCompanyname.Text & "'," & CboCompanyType.SelectedValue & ", '" & Me.TxtAdd1.Text & "'," & Me.cboTmb.SelectedValue & "," & Me.cboAMPHUR.SelectedValue & " , " & Me.CboProvince.SelectedValue
            Strsql = Strsql & ", '" & Me.TxtZipcode.Text & "', '" & Me.TxtTel1.Text & "','" & Me.TxtFax1.Text & "'"
            Strsql = Strsql & ",'" & Me.TxtNote.Text & "'," & Me.ChkActive.Checked & " ,'" & Me.TxtTaxID.Text & "','" & Me.TxtContactname.Text & "')"

            _Mysql.MySQLExecute(Strsql)



            Me.DialogResult = Windows.Forms.DialogResult.OK


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnSave_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub frmNewVendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Vendor_Type()
        Load_AddressData()
    End Sub

    Private Sub CboProvince_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboProvince.SelectedIndexChanged
        Try
            If On_Load = True Then Exit Sub
            Strsql = "Select amphur_code,amphur_name from tblamphur where amphur_code like '" & Me.CboProvince.SelectedValue.ToString & "%'"
            DTAMPHUR = _Mysql.GetMYSQLDataTable(Strsql, "tblAMPHUR")

            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTAMPHUR
                .DisplayMember = "amphur_name"
                .ValueMember = "amphur_code"
            End With
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
            ' Cboonload = False
        Catch ex As Exception

        End Try
      
    End Sub

    Private Sub cboAMPHUR_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAMPHUR.SelectedIndexChanged
        Try
            If On_Load = True Then Exit Sub
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
        Catch ex As Exception

        End Try
       
    End Sub
End Class